﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BUS;
using DTO;
using System.IO;
namespace GUI
{
    public partial class uc_TimKiemPK : DevExpress.XtraEditors.XtraUserControl
    {
        public uc_TimKiemPK()
        {
            InitializeComponent();
        }
        AccessoryBL accbl = new AccessoryBL();
        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            if (txtNoiDungTK.Text != "")
            {
                if (radioGroup1.SelectedIndex == 0)
                {
                    gridPhuKien.DataSource = accbl.Accessory_SelectByID(int.Parse(txtNoiDungTK.Text));
                }
                else if (radioGroup1.SelectedIndex == 1)
                {
                    gridPhuKien.DataSource = accbl.Accessory_SelectByName(txtNoiDungTK.Text);
                }
                else if (radioGroup1.SelectedIndex == 2)
                {
                    gridPhuKien.DataSource = accbl.Accessory_SelectByType(txtNoiDungTK.Text);
                }
            }
        }

        private void txtNoiDungTK_KeyPress(object sender, KeyPressEventArgs e)
        {
            Constraint.checkLenght(txtNoiDungTK, e, 50);
            if ((Keys)e.KeyChar == Keys.Enter)
                btnTimKiem_Click(null, null);
        }

        private void uc_TimKiemPK_Load(object sender, EventArgs e)
        {
            accessoryTableAdapter.Fill(sellingmobilemanagementDataSet3.accessory);
        }

        private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            txtTenPK.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Name").ToString();
            txtBaoHanh.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Brand").ToString();
            txtGiaBan.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Prize").ToString();
            txtMauSac.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Color").ToString();
            txtSoLuongTon.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Count").ToString();
            txtNhanHieu.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Brand").ToString();
            cbbLoaiPK.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Type").ToString();
            int imageid = Int32.Parse(gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "ImageID").ToString());
            ImageBL imgbl = new ImageBL();
            ImageDTO imgdto = new ImageDTO();
            imgdto = imgbl.Image_SelectByID(imageid);
            if (imgdto.Image == null)
                picPhuKien.Image = null;
            else
            {
                MemoryStream st = new MemoryStream();
                st.Write(imgdto.Image, 0, imgdto.Image.Length);
                Image i = Image.FromStream(st);
                picPhuKien.Image = resizeImage(i, picPhuKien.Width, picPhuKien.Height);
            }

        }
        public Image resizeImage(Image img, int width, int height)
        {
            Bitmap b = new Bitmap(width, height);
            Graphics g = Graphics.FromImage((Image)b);

            g.DrawImage(img, 0, 0, width, height);
            g.Dispose();

            return (Image)b;
        }
    }
}
